<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/24 0024
 * Time: 10:16
 */
$xml = xml_parser_create('UTF-8');

xml_set_element_handler($xml, 'start_handler', 'end_handler');
xml_set_character_data_handler($xml, 'character_handler');
xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, false);
$level = 0;
function start_handler($xml, $stag, $attributes)
{
    //echo '触发了吗';
    global $level;
    echo "\n" . str_repeat(' ', $level) . ">>>$stag";

    foreach ($attributes as $index => $attribute) {
        echo "$index == $attribute";
    }
    $level++;
}

function end_handler($xml, $stag)
{
    //echo '触发了吗';
    global $level;
    $level--;
    echo str_repeat(' ', $level) . "<<<$stag";
}

function character_handler($xml, $data)
{
//    echo '触发了吗';
    global $level;
    $data = explode("\n", wordwrap($data, 76 - ($level * 2)));
    foreach ($data as $line) {
        echo str_repeat(' ', ($level+1)) .$line ."\n";
    }
}
$res = xml_parse($xml,file_get_contents('./test.xml'));
var_dump($res);